package com.amex.uam.action;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amex.uam.model.Message;
import com.microstrategy.web.beans.MarkupOutput;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.tasks.AbstractBaseTask;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskParameterMetadata;
import com.microstrategy.web.tasks.TaskRequestContext;

public class EditUserTask extends AbstractBaseTask {

	public static final String SID_LOAD_USER_PROFILE = "LoadUserProfile";//Netezza
	public static final String SID_LOAD_ALL_PROJECTS = "LoadAllProjects";//Netezza
	public static final String SID_LOAD_USER_PROJ = "LoadUserProj";//Netezza
	public static final String SID_LOAD_GEO_CTRY = "LoadGEOCounrty";//Netezza
	public static final String SID_LOAD_GEO_RGN = "LoadGEORegion";//Netezza
	public static final String SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME = "LoadSecurityGroupByGroupName";//MD
	public static final String SID_LOAD_USER_SECURITY_GROUP = "LoadUserSecurityGroup";
	public static final String SID_EDIT_USERP_ROFILE = "EditUserProfile";

	static BeanFactory ctx = new ClassPathXmlApplicationContext("beans.xml");

	private TaskParameterMetadata taskSubID;

	public EditUserTask() {
		super("EditUserTask");
		taskSubID = addParameterMetadata("TaskSubID", "Enter Operation", true, "");
	}

	@Override
	public void processRequest(TaskRequestContext context, MarkupOutput markupOutput) throws TaskException {

		RequestKeys keys = context.getRequestKeys();
		String subId = keys.getValue(taskSubID.getName());

		WebIServerSession session = null;
		try {
			session = context.getWebIServerSession(null, "session");
		} catch (Throwable t) {
		}

		Message message = null;

		System.out.println("** EditUserTask->taskSub: " + subId);
		
		if (SID_LOAD_USER_PROFILE.equalsIgnoreCase(subId)) {
			message = ctx.getBean("userAction", UserAction.class).loadUserProfile(keys);
		} else if (SID_LOAD_ALL_PROJECTS.equalsIgnoreCase(subId)) {
			message = ctx.getBean("projAction", ProjAction.class).loadAllProjects();
		} else if (SID_LOAD_USER_PROJ.equalsIgnoreCase(subId)) {
			message = ctx.getBean("projAction", ProjAction.class).loadUserProjects(keys);
		} else if (SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME.equalsIgnoreCase(subId)) {
			message = ctx.getBean("mstrAction", MstrAction.class).loadSecurityGroupByGroupName(keys, session);
		}else if(SID_LOAD_GEO_CTRY.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEOCounrty();
		}else if(SID_LOAD_GEO_RGN.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEORegion();
		}else if(SID_LOAD_USER_SECURITY_GROUP.equals(subId)) {
			message = ctx.getBean("userAction", UserAction.class).loadUserSecurityGroup(keys);
		}else if(SID_EDIT_USERP_ROFILE.equalsIgnoreCase(subId)){
			/*
			int count = keys.getNameCount();
			System.out.println("** Edit User Profile Parameters:");
			for (int i = 0; i < count; i++) {
				String name = keys.getName(i);
				String value = keys.getValue(name);
				System.out.println(name + " = " + value);
			}
			System.out.println("********************************");*/
			message = ctx.getBean("editUserAction", EditUserAction.class).editUser(keys);
		}else {
			message = new Message(Message.ERROR_SUBID, "Invalid TaskSubID: " + subId);
		} 

		if (message != null) {
			// System.out.println(message.toJSON());
			markupOutput.append(message.toJSON());
		} else {
			System.out.println("Nothing respond!");
			markupOutput.append("");
		}
	}
}
